Imports System.Text.RegularExpressions
Imports System.Collections.Generic

Public Class SharePointContainer

    Enum TypeEnum
        None = 0
        Site = &H1
        Library = &H10
        Folder = &H100
    End Enum

    Private _canContainDocuments As Boolean
    Private _id As String
    Private _name As String
    Private _path As Uri
    Private _type As SharePointContainer.TypeEnum

    Sub New()
        _canContainDocuments = False
        _id = String.Empty
        _name = String.Empty
        _type = SharePointContainer.TypeEnum.None
    End Sub

    Sub New(ByVal canContainDocuments As Boolean, _
            ByVal id As String, _
            ByVal name As String, _
            ByVal path As Uri, _
            ByVal type As SharePointContainer.TypeEnum)

        _canContainDocuments = canContainDocuments
        _id = id
        _name = name

        _path = New Uri(Regex.Replace(path.ToString(), "[ /]$", String.Empty) + "/")
        _type = type
    End Sub

    Public Property CanContainDocuments() As Boolean
        Get
            Return _canContainDocuments
        End Get
        Friend Set(ByVal value As Boolean)
            _canContainDocuments = value
        End Set
    End Property

    Public Property Id() As String
        Get
            Return _id
        End Get
        Friend Set(ByVal value As String)
            _id = value
        End Set
    End Property

    Public Property Name() As String
        Get
            Return _name
        End Get
        Friend Set(ByVal value As String)
            _name = value
        End Set
    End Property

    Public Property Path() As Uri
        Get
            Return _path
        End Get
        Friend Set(ByVal value As Uri)
            _path = value
        End Set
    End Property

    Public Property ContainerType() As SharePointContainer.TypeEnum
        Get
            Return _type
        End Get
        Friend Set(ByVal value As SharePointContainer.TypeEnum)
            _type = value
        End Set
    End Property

End Class

Public Class SharePointDocument

    Private _location As SharePointLocation
    Private _spDocumentClass As SharePointContentType
    Private _metaData As New List(Of SharePointField)

    Public Property SharePointDocumentClass() As SharePointContentType
        Get
            Return _spDocumentClass
        End Get
        Set(ByVal value As SharePointContentType)
            _spDocumentClass = value
        End Set
    End Property

    Public ReadOnly Property MetaData() As List(Of SharePointField)
        Get
            Return _metaData
        End Get
    End Property

    Public Property DocumentLocation() As SharePointLocation
        Get
            Return _location
        End Get
        Set(ByVal value As SharePointLocation)
            _location = value
        End Set
    End Property
End Class

Friend Class SharePointFolder
    Inherits SharePointContainer

    Sub New()

        MyBase.New()
    End Sub

    Sub New(ByVal siteUrl As Uri, ByVal e As System.Xml.Linq.XElement)
        MyBase.New(True, _
                   GetValueFromIdValue(e.Attribute("ows_UniqueId").Value), _
                   GetValueFromIdValue(e.Attribute("ows_FileLeafRef").Value), _
                   New Uri(siteUrl, GetValueFromIdValue(e.Attribute("ows_FileRef").Value)), _
                   GetContentType(e))

        Dim i = e.Attribute("ows_ID").Value
    End Sub

    Private Shared Function GetValueFromIdValue(ByVal idValue As String) As String
        Const ID_VALUE_SEPARATOR As Char = "#"c

        Return idValue.Substring(idValue.IndexOf(ID_VALUE_SEPARATOR) + 1)
    End Function

    Private Shared Function GetContentType(ByVal e As System.Xml.Linq.XElement) As SharePointContainer.TypeEnum
        Dim contentType As SharePointContainer.TypeEnum

        Dim id As String
        id = GetValueFromIdValue(e.Attribute("ows_UniqueId").Value)
        contentType = SharePointContainer.TypeEnum.Folder
        Return contentType

    End Function

End Class

Public Class SharePointLibrary
    Inherits SharePointContainer

    <Flags()> _
    Enum LibraryBaseType
        Any = 0
        GenericList = &H1
        DocumentLibrary = &H10
        DiscussionBoard = &H100
        Survey = &H1000
        Issue = &H10000
    End Enum

    Private _baseType As LibraryBaseType
    Private _hidden As Boolean = False
    Private _fields As New List(Of SharePointField)

    Sub New()

        MyBase.New()
    End Sub

    Sub New(ByVal canContainDocuments As Boolean, _
            ByVal id As String, _
            ByVal name As String, _
            ByVal siteUrl As Uri, _
            ByVal type As SharePointContainer.TypeEnum)

        MyBase.New(canContainDocuments, id, name, siteUrl, type)
    End Sub

    Public Property BaseType() As LibraryBaseType
        Get
            Return _baseType
        End Get
        Friend Set(ByVal value As LibraryBaseType)
            _baseType = value
        End Set
    End Property

    Public Property Hidden As Boolean
        Get
            Return _hidden
        End Get
        Friend Set(ByVal value As Boolean)
            _hidden = value
        End Set
    End Property

    Public ReadOnly Property Fields() As List(Of SharePointField)
        Get
            Return _fields
        End Get
    End Property
End Class
